package com.factorit.utils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateConverter {
	public final static String FORMATO_FECHA_HORA = "dd/MM/yyyy, HH:mm";
	public final static String FORMATO_FECHA = "dd/MM/yyyy";
	public final static String FORMATO_HORA = "HH:mm";

	// convert date to string date and time
	public String convertDateToDateStringWithTime(Date date) {
		SimpleDateFormat sdf = new SimpleDateFormat(FORMATO_FECHA_HORA);
		String dateString = sdf.format(date);
		return dateString;
	}

	// convert date to string with format parameter
	public String convertDateToString(Date date, String format) {
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		String dateString = sdf.format(date);
		return dateString;
	}

	// convert calendar to string date and time
	public String convertCalendarToDateStringWithTime(Calendar calendar) {
		SimpleDateFormat sdf = new SimpleDateFormat(FORMATO_FECHA_HORA);
		String dateString = sdf.format(calendar.getTime());
		return dateString;
	}

	// convert calendar to string date
	public String convertCalendarToDateString(Calendar calendar) {
		SimpleDateFormat sdf = new SimpleDateFormat(FORMATO_FECHA);
		String dateString = sdf.format(calendar.getTime());
		return dateString;
	}

	// convert a string date to calendar
	public Calendar convertDateStringToCalendar(String dateString) throws ParseException {
		Date date;
		DateFormat formatter = new SimpleDateFormat(FORMATO_FECHA);
		date = (Date) formatter.parse(dateString);
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		return cal;
	}

	// convert a string date to date
	public Date convertDateStringToDate(String dateString) throws ParseException {
		Date date;
		DateFormat formatter = new SimpleDateFormat(FORMATO_FECHA);
		date = (Date) formatter.parse(dateString);
		return date;
	}

	// convert a string date and string time to calendar
	public Calendar convertDateStringToCalendar(String dateString, String timeString) throws ParseException {

		Date date;
		String dateStr = dateString + " " + timeString;
		DateFormat formatter = new SimpleDateFormat(FORMATO_FECHA_HORA);
		date = (Date) formatter.parse(dateStr);
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		return cal;
	}

	public static boolean isToday(Date aDate) {
		Calendar aCal1 = Calendar.getInstance();
		aCal1.setTime(aDate);

		Date date1 = new Date();

		Calendar aCal2 = Calendar.getInstance();
		aCal2.setTime(date1);

		if ((aCal1.get(Calendar.DATE) == aCal2.get(Calendar.DATE))
				&& (aCal1.get(Calendar.YEAR) == aCal2.get(Calendar.YEAR))
				&& (aCal1.get(Calendar.MONTH) == aCal2.get(Calendar.MONTH))) {
			return true;
		} else {
			return false;
		}
	}

}
